package org.lemon.db.tx;

import org.lemon.db.store.Page;

import java.nio.charset.StandardCharsets;

/**
 * @since 2024-12-13 11:33
 */
public abstract class LogRecord {
    protected final static int TX_OFFSET = 4;
    protected int txNum;

    public LogRecord() {
    }

    public LogRecord(int txNum) {
        this.txNum = txNum;
    }

    abstract RecordType op() ;
    protected int txNumber() {
        return txNum;
    }

    protected void undo(Transaction transaction) {

    }

    protected byte[] data() {
        return op().name().getBytes(StandardCharsets.UTF_8);
    }

    protected int decode(Page p ){
        this.txNum = p.getInt(TX_OFFSET);
        return 8;
    }

    protected byte[] encode(){
        byte[] record = new byte[8];
        Page page = Page.create(record);
        page.setInt(0, op().getCode());
        page.setInt(TX_OFFSET,txNumber());
        return record;
    }


}
